//index.js
import FiveStone from '../../FiveStone/FiveStone';

(function () {
  //获取应用实例
  const app = getApp();
  /**
   * 胜利之后的回调
   */
  function winCallback(winStone, startLoc, endLoc) {
    const page = getCurrentPages()[0];
    const startLocation = this.chessBoard[startLoc.x][startLoc.y].pos;
    const endLocation = this.chessBoard[endLoc.x][endLoc.y].pos;
    const long = Math.sqrt(Math.pow(startLocation.x - endLocation.x, 2) +
                           Math.pow(startLocation.y - endLocation.y, 2));
    const tipAngle = Math.acos((endLocation.x - startLocation.x) / long);
    console.log(tipAngle);
    console.log(100);
    page.setData({
      winLoc:{
        start:startLocation,
        end:endLocation,
        width:long,
        angle:tipAngle
      }
    });
    //已经成功以后禁止再下子
    page.fiveStone.preventStep();
  }
  let audioAction1=app.globalData.audioAction
  let Button=app.globalData.audioButton
  Page({
    data: {
      userInfo: {},
      hasUserInfo: false,
      fiveStone:'',
      canIUse: wx.canIUse('button.open-type.getUserInfo'),
      imageUrl1:"https://www.easyicon.net/api/resizeApi.php?id=1252772&size=128",
      imageUrl2:"https://www.easyicon.net/api/resizeApi.php?id=1223077&size=128",
      showStepTip:false,
      stepTipPos:{
        x:0,
        y:0
      },
      winLoc:{
        start:null,
        end:null
      },
      current1: {
        src: 'https://onedrive.gimhoy.com/1drv/aHR0cHM6Ly8xZHJ2Lm1zL3UvcyFBcl9md0ZDWGtDRmJoMnBiNlctNFp6TWhESlprP2U9ZkR4cWJE.mp3',
      },
      current2: {
        src: 'https://onedrive.gimhoy.com/1drv/aHR0cHM6Ly8xZHJ2Lm1zL3UvcyFBcl9md0ZDWGtDRmJoMmNGZ1FEXzFtRmlZOERTP2U9RWVvdVZO.mp3',
      },
      audioAction: {
        method: 'play'
      },
      audioButton: {
        method: 'pause'
      }
    },
    /**
   * 控制下子提示的视图显示
   */
    handleTip:function(e) {
      const page = getCurrentPages()[0];
      if (!page.fiveStone.canStep()) {
        return;
      }
      const stepTipPos = page.fiveStone.getStepPosition(e);
      page.loc = page.fiveStone.getStepLocation(e);
      //如果没有获取到下子的位置，隐藏掉下子提示
      if (stepTipPos == null) {
        page.setData({
          showStepTip:false
        });
        return;
      }
      //设置并显示下子提示的位置
      page.setData({
        'stepTipPos':stepTipPos,
        showStepTip:true
      });
    },
    AIstep:function(){
      const page = getCurrentPages()[0];
      page.fiveStone.AIstep();
    },
    onChessBoardTouchStart: function (e) {
      this.handleTip(e);
      if(Button.method=='play')
      {
        this.setData({
          audioButton: {
            method: 'play'
          }
        });
      }
      else
      {
        this.setData({
          audioButton: {
            method: 'pause'
          }
        });
      }
    },
    onChessBoardTouchMove: function (e) {
      this.handleTip(e);
    },
    onChessBoardTouchEnd: function (e) {
      const page = getCurrentPages()[0];
      const loc = this.loc;
      if (loc != null) {
        if (!page.fiveStone.step(loc.x, loc.y)) return ;
        this.refreshFiveStone();
        //this.handleTip(e);
        var stepTipPos=page.fiveStone.AIstep();
        this.refreshFiveStone();
        page.setData({
          'stepTipPos':stepTipPos,
          showStepTip:true
        });
      }
    },
    /**
     * 重来
     */
    restart: function () {
      this.setData({
        showStepTip:false
      });
      this.fiveStone.restart();
      this.refreshFiveStoneAndClearWinTip();
    },

    leave: function(){
      wx.redirectTo({
        url: '../menu/menu',
      })
    },
    leave: function(){
      wx.redirectTo({
        url: '../menu/menu',
      })
    },
    /**
     * 悔棋
     */
    undo: function () {
      this.setData({
        showStepTip:false
      });
      this.fiveStone.undo();
      this.fiveStone.undo();
      this.refreshFiveStoneAndClearWinTip();
    },
    refreshFiveStone: function () {
      this.setData({
        'fiveStone':this.fiveStone
      });
    },
    refreshFiveStoneAndClearWinTip: function () {
      this.setData({
        'fiveStone':this.fiveStone,
        'winLoc':{
          start:null,
          end:null
        }
      });
    },
    //事件处理函数
    bindViewTap: function() {
      wx.navigateTo({
        url: '../logs/logs'
      })
    },
    onLoad: function () {
      if(audioAction1.method=='play'){
        this.setData({
          audioAction: {
            method: 'play'
          }
        });
      }else {
        this.setData({
          audioAction: {
            method: 'pause'
          }
        });
      }
      console.log('onLoad')
      //初始化棋盘
      this.fiveStone = new FiveStone(15, 0.9);
      this.fiveStone.setWinCallback(winCallback);
      //这里不使用refreshFiveStone的原因是因为这里是初始化，区别出后面的刷新
      this.setData({
        'fiveStone':this.fiveStone
      });


      this.loc = null;

      var that = this;
      //调用应用实例的方法获取全局数据
      if (app.globalData.userInfo) {
        this.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfo: true
        })
      } else if (this.data.canIUse){
        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        app.userInfoReadyCallback = res => {
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      } else {
        // 在没有 open-type=getUserInfo 版本的兼容处理
        wx.getUserInfo({
          success: res => {
            app.globalData.userInfo = res.userInfo
            this.setData({
              userInfo: res.userInfo,
              hasUserInfo: true
            })
          }
        })
      }
    },
    getUserInfo: function(e) {
      console.log(e)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
  })
  
})();
